package se.peterbjorkman.android.trafikkamera;

import se.peterbjorkman.android.trafikkamera.infodownload.DownloadCameraInfoTask;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;

public class MainActivity extends TabActivity {
	
	private static final int UPDATE_MENU_ID = Menu.FIRST;
	private static final int ABOUT_MENU_ID = UPDATE_MENU_ID + 1;
	public static final int LAAT_USED_MENUID = ABOUT_MENU_ID;

	private static final int DIALOG_WELCOME = 0;
	private static final int DIALOG_ABOUT = DIALOG_WELCOME + 1;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final TabHost tabHost = getTabHost();

        // TODO Hardcoded strings
        tabHost.addTab(tabHost.newTabSpec("tab1")
                .setIndicator(getString(R.string.Routes))
                .setContent(new Intent(this, RoutesCameraActivity.class)));

        tabHost.addTab(tabHost.newTabSpec("tab2")
                .setIndicator(getString(R.string.Map))
                .setContent(new Intent(this, CameraMapActivity.class)));
        
        tabHost.addTab(tabHost.newTabSpec("tab3")
                .setIndicator(getString(R.string.ByCounty))
                .setContent(new Intent(this, CountyCameraActivity.class)));
        
        if (!RoutesCameraActivity.routeExists(this))
        	tabHost.setCurrentTab(2);
        if (WelcomeDialogBuilder.ShouldBeShown(this)){
            showDialog(DIALOG_WELCOME);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Log.d(LOGTAG, "In onCreateOptionsMenu");
        
        super.onCreateOptionsMenu(menu);

        menu.add(0, UPDATE_MENU_ID, 0, "Uppdatera")
            ;//.setIcon(R.drawable.menu_update);
        
        menu.add(0, ABOUT_MENU_ID, 0, R.string.about)
        	.setIcon(android.R.drawable.ic_menu_info_details);
        
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //Log.d(LOGTAG, "In onOptionsItemSelected");
     
        switch (item.getItemId()) {
        case UPDATE_MENU_ID:
        	StartDbUpdate();
            return true;
        case ABOUT_MENU_ID:
            showDialog(DIALOG_ABOUT);
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_WELCOME: 
            WelcomeDialogBuilder builder = new WelcomeDialogBuilder(this);
            return builder.getWelcomeDialog();
        case DIALOG_ABOUT: 
            AboutDialogBuilder aBuilder = new AboutDialogBuilder(this);
            return aBuilder.getAboutDialog();
        }
        return null;
    }
    
	private void StartDbUpdate() {
		DownloadCameraInfoTask task = new DownloadCameraInfoTask(this);
		task.execute("http://trafikdata.peterbjorkman.se/cctv/getRoadCameraInfo.php");
	}
    
}
